package com.feib.ebilling.job;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.feib.ebilling.job.service.BillFeedbackAutoloansService;
import com.feib.ebilling.job.service.FtpService;

/**
 * @title (#)BankingFeedbackAutoloans.java<br>
 * @description 車貸策略聯盟超商、郵局代收回饋優利規格檔<br>
 * @author Fiona Huang<br>
 * @version 1.0.0 2011/11/07
 * @copyright Far Eastern International Bank Copyright (c) 2011<br>
 * @2011/11/07 create by Fiona Huang<br>
 * @20121225 因為測試套有很多業者虛擬帳號4碼(or 3碼)一樣，所以會產生重覆資料
 */

@Service("bankingFeedbackAutoloans")
public class BankingFeedbackAutoloans {

	public static Logger logger = LoggerFactory.getLogger(BankingFeedbackAutoloans.class);

	public static final SimpleDateFormat BASIC_DATE_TIME_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

	@Resource(name = "billFeedbackAutoloansService")
	private BillFeedbackAutoloansService billFeedbackAutoloansService;

	@Resource(name = "ftpService")
	private FtpService ftpService;

	public static void main(String[] args) {

		/* 傳輸日期 --> 銷帳日期當日 */
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		Date dNow = new Date();		
		String processDate = dateFormat.format(dNow);
		
		if (null == args || 0 == args.length) {
			/* 處理系統日當日車貸超商、郵局代收回饋 */
			// do nothing.			
		} else {
			/*指定日期參數 */
			processDate = args[0];
		}

		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				new String[] { "applicationContext-config.xml", "applicationContext-dao.xml",
						       "applicationContext-properties.xml" });

		Date start = new Date();
		logger.info("[開始] 車貸策略聯盟超商、郵局代收回饋優利  - {}", BASIC_DATE_TIME_FORMAT.format(start));
		try {
			/* 取得交易物件 */
			BankingFeedbackAutoloans billFeedback = (BankingFeedbackAutoloans) ctx.getBean("bankingFeedbackAutoloans");
			/* 依日期產生檔案 */
			billFeedback.doProcessDate(processDate);			
		} catch (Exception e) {
			logger.error("[錯誤]" + e.getMessage(), e);
		} finally {
			Date end = new Date();
			logger.info("[結束] 車貸策略聯盟超商、郵局代收回饋優利  - {}", BASIC_DATE_TIME_FORMAT.format(start));
			logger.info("共費時 - " + ((end.getTime() - start.getTime()) / 1000)	+ " 秒");
		}
	}
	
	public void doProcessDate(String processDate) throws Exception{
    	/* 產生銷帳檔案 */
        try {
        	execute(processDate);
        }catch (Exception e) {
            logger.error("[錯誤]" + e.toString(), e);
        }
    }
	
	public void execute(String processDate) throws IOException{
       
        Assert.notNull(processDate, "[錯誤]傳輸日期  can't be null.");
        logger.info("************************************************");
        logger.info("傳輸日期：{}", processDate);
        logger.info("傳輸類別：車貸策略聯盟超商、郵局代收回饋優利");
        String sContent = this.billFeedbackAutoloansService.writeContent("4", processDate);
        /* 上傳資料 */
        ftpService.putFTPFile("FEIB", processDate, sContent, "CarLoan_Store" + "." + processDate);
   }
}
